package com.googlecode.animator.controller
{
	import com.googlecode.animator.data.markers.KeyframeVO;
	import com.googlecode.animator.data.objects3d.Object3DVO;
	import com.googlecode.caurina.transitions.Tweener;
	
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	
	import org.papervision3d.core.math.Number3D;
	import org.puremvc.as3.interfaces.INotification;

	/**
	 * Calculates the tweens between keyframes. 
	 * @author USER
	 * 
	 */
	public class TimelineCalculateTweensCommand extends BaseCommand
	{
		/**
		 * Execute command. 
		 * @param notification
		 * 
		 */
		override public function execute(notification:INotification):void
		{
			var VO:Object3DVO = settings.getObject3D(notification.getBody() as String);
			var keyframes:Dictionary = VO.keyframes;

			var keyframesAC:ArrayCollection = new ArrayCollection();
			
			for each(var kf:KeyframeVO in keyframes)
			{
				keyframesAC.addItem(kf);
			}
			
			//Sort keyFrames time ascending
			var s:Sort = new Sort();
			var sF:SortField = new SortField("time", true);
			sF.numeric = true;				
			s.fields = [sF];
			keyframesAC.sort = s;
			keyframesAC.refresh();
			
			//Tween
			if(keyframesAC.length < 1)
			{
				return;
			}
			
			var evt:Event = new Event("event");
			var startPos:Number3D = ((keyframesAC[0] as KeyframeVO).position).clone();
			
			settings.removeAllTweens(VO.name);
			
			settings.addTween(VO.name, startPos);
			
			/* for(var i:uint=0; i<keyframesAC.length-1; i++)
			{
				var currentPos:Number3D = (keyframesAC[i].position as Number3D).clone();
				var nextPos:Number3D = (keyframesAC[i+1].position as Number3D).clone();
				var t:uint = (keyframesAC[i+1].time - keyframesAC[i].time);

				if(t==0)
				{
					return;
				}
				
				Tweener.addTween(currentPos, {x:nextPos.x, y:nextPos.y, z:nextPos.z, time:t, useFrames:true, transition:"linear"});
				
				for(var j:uint=0; j<t; j++)
				{
					Tweener.onEnterFrame(evt);
					
					settings.addTween(VO.name, currentPos);
					
				}
				
			} */
			
			//Tweener.removeTweens(currentPos);
		} 
		
	}
}